package com.hnevc.xiaoming.shijianactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.Toast;

public class TouchActivity extends AppCompatActivity {

    private MyButton myButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_touch);

        myButton = new MyButton(this);
        myButton.setText("Hello");
        myButton.setTextSize(20);
        myButton.setAllCaps(false);
        setContentView(myButton);
    }


    @SuppressLint("AppCompatCustomView")
    class MyButton extends Button{


        public MyButton(Context context) {
            super(context);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()){
                case  MotionEvent.ACTION_DOWN:
                    Toast.makeText(TouchActivity.this, "按钮按下", Toast.LENGTH_SHORT).show();
                    break;
                case  MotionEvent.ACTION_UP:
                    Toast.makeText(TouchActivity.this, "按钮弹起", Toast.LENGTH_SHORT).show();
                    break;
                case  MotionEvent.ACTION_MOVE:
                    Toast.makeText(TouchActivity.this, "在按钮按上移动", Toast.LENGTH_SHORT).show();
                    break;
            }
            return super.onTouchEvent(event);
        }
    }
}